Skip to content

[BugFix] honor collector masks in loss reductions - #4057

Open
gtnv wants to merge 7 commits into
pytorch:mainfrom
gtnv:fix/loss-mask
Open

[BugFix] honor collector masks in loss reductions#4057
gtnv wants to merge 7 commits into
pytorch:mainfrom
gtnv:fix/loss-mask

Conversation

@gtnv

@gtnv gtnv commented Aug 2, 2026

Copy link
Copy Markdown

#3888 refactored objectives to route through _reduce_loss but the collector mask change from #3850 never merged, leaving a gap. The relevant code changed as well. This made it so padded timesteps from collector, mask were still included and could change the final loss and grads.

BC & RND use it directly. A2C, ppo and reinforce pass input via TensorDict and REDQ/OnlineDT deal with shape-specific cases their own way.

closes #3866.

Ran tests; 9314 passed.

cc @vmoens

Tested

test/objectives: 7525 passed, 2330 skipped, matching the pre-PR baseline
(7522 passed with the same skips, plus the 3 new regression tests).

Worth flagging: before the fix in 4cc1e15, this branch failed 72 existing
tests
— every TestTD3::test_td3_batcher and TestTD3BC::test_td3bc_batcher
parametrization. Their fixtures supply ("collector", "mask") with the standard
trailing-singleton shape [B, T, 1] against a [B, T] loss, and
_expand_loss_mask only handled masks with fewer dimensions than the loss, so
expand_as raised. shifted_valid never hit this because the value estimators
emit it already squeezed. That crash is the reason a mask-discovery change needs
the objectives suite run against a baseline, not just new unit tests.

@pytorch-bot

pytorch-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4057

Note: Links to docs will display an error until the docs builds have been completed.

❌ 7 New Failures

As of commit 42a022e with merge base 7330afb (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 2, 2026
@gtnv
gtnv marked this pull request as ready for review August 2, 2026 01:23

@theap06 theap06 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @vmoens - can you trigger github workflows?

@theap06 theap06 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some feedback cc @vmoens

Comment thread torchrl/objectives/common.py Outdated
Comment thread torchrl/objectives/redq.py
Comment thread torchrl/objectives/common.py
@vmoens

vmoens commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

I made some edits in 77518041d2a04a66c4807c8bf2b75d4b43e0d8ed, see the comments in the commit.

@vmoens vmoens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Aug 4, 2026
gtnv and others added 7 commits August 5, 2026 16:12
PR pytorch#3888 routed objectives through _reduce_loss, but the helper still ignored ("collector", "mask") because the corresponding change in pytorch#3850 never merged. This fix excludes padded timesteps from loss reduction so they do not affect the loss or gradients.
Unify unreduced mask behavior. Apply collector masks to REDQ under reduction="none". Remove unreachable mask-shape handling.
Keeps the pytorch#3866 design -- discovery of the validity masks TorchRL writes itself
is the default, so switching to SliceSampler(pad_output=True) needs no per-loss
wiring -- and adds the escape hatch it was missing, plus the coverage and the
robustness fix.

- `LossModule.loss_mask_key` (new, default `"auto"`) selects the mask source:
  `"auto"` reads every entry of `AUTO_LOSS_MASK_KEYS` and ANDs the ones it
  finds; a `NestedKey` restricts masking to that single entry, resolving which
  mask applies when a batch carries both; `None` disables masking. Exposed as an
  attribute rather than a constructor kwarg so it needs no matching field on
  every `*LossConfig` (CLAUDE.md section 14).

- Masked positions are now selected out with `torch.where` on every reduction
  path, not multiplied by a zeroed mask: `nan * 0` is `nan` in both the forward
  and the backward pass, so a non-finite value at a masked position used to
  poison the loss and the gradients.

- Tests: `TestLossMaskReduction` in test/objectives/test_loss_module.py pins the
  three mask-source modes, the AND of the auto keys, no-op reduction on unmasked
  data, non-finite masked positions, trailing-dim broadcasting, and both
  `reduction="none"` contracts. `test_collector_mask` /
  `test_collector_mask_can_be_disabled` in test/objectives/test_bc.py cover a
  real loss end to end.

- Docs: a "Masked reduction" section in objectives_common.rst, and
  `AUTO_LOSS_MASK_KEYS` exported from `torchrl.objectives`.

Reverts the unannounced `BCLoss` change that turned an all-padding batch from 0
into `nan`: worth doing, but it is a separate behavior change that needs its own
deprecation note and test.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Running the objectives suite locally shows the collector-mask discovery breaks
72 existing tests: `("collector", "mask")` is [B, T, 1] (masks carry a trailing
singleton to broadcast against [..., 1]-shaped rewards) while a per-timestep
loss is [B, T], and _expand_loss_mask only handled masks with *fewer* dims than
the loss, so expand_as raised. TD3/TD3+BC batcher tests cover exactly this
shape.

_expand_loss_mask now drops trailing singleton dimensions before broadcasting,
and raises a clear ValueError when the mask has genuinely more non-singleton
dimensions than the loss. Regression tests for both, parametrized over the two
auto keys.

test/objectives: 7522 passed, matching the pre-PR baseline exactly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vmoens vmoens changed the title [bugfix]: honor collector masks in loss reductions [BugFix] honor collector masks in loss reductions Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Documentation Improvements or additions to documentation Objectives

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Mask-aware reduction across LossModule subclasses

3 participants